package com.erbao.thread.juc;

import com.erbao.datastructurearithmetic.RandomUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * @author CaoBaoYin
 * @since 22/3/9 11:15
 */
public class ParallelStreamTest {

    public static void main(String[] args) {
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < 1000000; i++) {
            list.add(RandomUtil.equalProbabilityInt(10000000));
        }

        long start = System.currentTimeMillis();
        list.forEach(ParallelStreamTest::isPrime);
        long end = System.currentTimeMillis();
        System.out.println(end - start);


        // list.stream().parallel().forEach(ParallelStreamTest::isPrime);
        list.parallelStream().forEach(ParallelStreamTest::isPrime);
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - end);
    }

    private static boolean isPrime(Integer value) {
        for (int i = 0; i < value / 2; i++) {
            if (value % 2 == 0) return false;
        }
        return true;
    }

}
